package com.tb.ClassSchedule;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.larswerkman.holocolorpicker.ColorPicker;
import com.larswerkman.holocolorpicker.SaturationBar;
import com.larswerkman.holocolorpicker.ValueBar;

/**
 * Created by guich on 2016/8/13.
 */
public class SetThemeActivity extends AppCompatActivity {


    private Context context;

    private Button colorButton1;
    private Button colorButton2;
    private Button colorButton3;
    private Button colorButton4;
    private Button colorButton5;
    private Button colorButton6;
    private Button colorButton7;
    private Button colorButton8;
    private Button colorButton9;
    private Button colorButton10;

    private LinearLayout colorExample;
    private int selectedColor;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_theme);
        context = this;

        initTitle();

        colorExample = (LinearLayout) findViewById(R.id.color_example);
        int color = MainActivity.sharedPreferences.getInt("color",MainActivity.DEFAULT_THEME);
        colorExample.setBackgroundColor(color);

        initColorButton();
        initColorPicker(color);



    }

    private void initTitle(){
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("设置主题");
        setSupportActionBar(toolbar);
        //显示返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            toolbar.setPadding(0,MainActivity.statusBarHeight,0,0);
        }
        toolbar.setBackgroundColor(MainActivity.sharedPreferences.getInt("color",MainActivity.DEFAULT_THEME));
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if(item.getItemId() == R.id.action_commit){
                    SharedPreferences.Editor editor = MainActivity.sharedPreferences.edit();
                    editor.putInt("color",selectedColor);
                    editor.commit();
                    editor.clear();
                    MainActivity.main.setBgColor(selectedColor);
                    ((Activity)context).finish();
                }
                return true;
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            //处理返回按钮
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_commit,menu);
        return true;
    }

    private void initColorButton(){

        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int color = ContextCompat.getColor(context, (int) view.getTag());
                colorExample.setBackgroundColor(color);
                selectedColor = color;
            }
        };

        colorButton1 = (Button) findViewById(R.id.color_1);
        colorButton1.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor1));
        colorButton1.setTag(R.color.defaultColor1);
        colorButton1.setOnClickListener(l);

        colorButton2 = (Button) findViewById(R.id.color_2);
        colorButton2.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor2));
        colorButton2.setTag(R.color.defaultColor2);
        colorButton2.setOnClickListener(l);

        colorButton3 = (Button) findViewById(R.id.color_3);
        colorButton3.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor3));
        colorButton3.setTag(R.color.defaultColor3);
        colorButton3.setOnClickListener(l);

        colorButton4 = (Button) findViewById(R.id.color_4);
        colorButton4.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor4));
        colorButton4.setTag(R.color.defaultColor4);
        colorButton4.setOnClickListener(l);

        colorButton5 = (Button) findViewById(R.id.color_5);
        colorButton5.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor5));
        colorButton5.setTag(R.color.defaultColor5);
        colorButton5.setOnClickListener(l);

        colorButton6 = (Button) findViewById(R.id.color_6);
        colorButton6.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor6));
        colorButton6.setTag(R.color.defaultColor6);
        colorButton6.setOnClickListener(l);

        colorButton7 = (Button) findViewById(R.id.color_7);
        colorButton7.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor7));
        colorButton7.setTag(R.color.defaultColor7);
        colorButton7.setOnClickListener(l);

        colorButton8 = (Button) findViewById(R.id.color_8);
        colorButton8.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor8));
        colorButton8.setTag(R.color.defaultColor8);
        colorButton8.setOnClickListener(l);

        colorButton9 = (Button) findViewById(R.id.color_9);
        colorButton9.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor9));
        colorButton9.setTag(R.color.defaultColor9);
        colorButton9.setOnClickListener(l);

        colorButton10 = (Button) findViewById(R.id.color_10);
        colorButton10.setBackgroundColor(ContextCompat.getColor(context, R.color.defaultColor10));
        colorButton10.setTag(R.color.defaultColor10);
        colorButton10.setOnClickListener(l);

    }

    public void initColorPicker(int oldColor){
        ColorPicker   picker = (ColorPicker) findViewById(R.id.picker);
        SaturationBar sBar   = (SaturationBar) findViewById(R.id.s_bar);
        ValueBar      vBar   = (ValueBar) findViewById(R.id.v_bar);
//        OpacityBar    oBar   = (OpacityBar) findViewById(R.id.o_bar);

        picker.addSaturationBar(sBar);
        picker.addValueBar(vBar);
        picker.setShowOldCenterColor(false);
        picker.setColor(oldColor);
        selectedColor = oldColor;
//        picker.addOpacityBar(oBar);
        picker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
            @Override
            public void onColorChanged(int color) {
                selectedColor = color;
                colorExample.setBackgroundColor(color);
            }
        });
    }

}
